<template>
  <nav class="navbar">
    <div class="container">
      <!-- Logo 和网站标题 -->
      <div class="navbar-brand">
        <router-link to="/" class="logo-link">
          <div class="logo">
            <svg width="32" height="32" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
              <path d="M21 15a2 2 0 0 1-2 2H7l-4 4V5a2 2 0 0 1 2-2h14a2 2 0 0 1 2 2z"></path>
            </svg>
          </div>
          <h1 class="site-title">AI角色扮演平台</h1>
        </router-link>
      </div>

      <!-- 导航链接 -->
      <div class="navbar-links">
        <router-link to="/" class="nav-link">首页</router-link>
        <router-link to="/characters" class="nav-link">探索角色</router-link>
        <router-link to="/about" class="nav-link">关于我们</router-link>
      </div>

      <!-- 右侧操作区 -->
      <div class="navbar-actions">
        <div v-if="isAuthenticated" class="user-menu">
          <button @click="toggleUserMenu" class="user-menu-button">
            <div class="avatar-placeholder">
              <span>{{ userNameInitial }}</span>
            </div>
            <span class="user-name">{{ userName }}</span>
            <svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
              <polyline points="6 9 12 15 18 9"></polyline>
            </svg>
          </button>
          <div v-if="showUserMenu" class="user-dropdown">
            <router-link to="/profile" class="dropdown-item">个人资料</router-link>
            <router-link to="/settings" class="dropdown-item">设置</router-link>
            <button @click="logout" class="dropdown-item logout">退出登录</button>
          </div>
        </div>
        <router-link to="/login" class="btn-login" v-else>登录</router-link>
      </div>
    </div>
  </nav>
</template>

<script setup>
import { ref, computed } from 'vue';
import { useRouter } from 'vue-router';
import { authStore } from '../store/auth';

const authStoreRef = authStore();
const router = useRouter();
const showUserMenu = ref(false);

// 计算属性
const isAuthenticated = computed(() => authStoreRef.isAuthenticated);
const userName = computed(() => authStoreRef.user?.username || '用户');
const userNameInitial = computed(() => userName.value.charAt(0).toUpperCase());

// 方法
const toggleUserMenu = () => {
  showUserMenu.value = !showUserMenu.value;
};

const logout = () => {
  authStoreRef.logout();
  router.push('/login');
  showUserMenu.value = false;
};

// 点击其他区域关闭下拉菜单
const handleClickOutside = (event) => {
  const userMenuButton = event.target.closest('.user-menu-button');
  const userDropdown = event.target.closest('.user-dropdown');
  
  if (!userMenuButton && !userDropdown) {
    showUserMenu.value = false;
  }
};

// 监听点击事件
window.addEventListener('click', handleClickOutside);
</script>

<style scoped>
.navbar {
  background-color: white;
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.08);
  position: sticky;
  top: 0;
  z-index: 1000;
}

.container {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 0 20px;
  max-width: 1200px;
  margin: 0 auto;
}

.navbar-brand {
  display: flex;
  align-items: center;
}

.logo-link {
  display: flex;
  align-items: center;
  text-decoration: none;
  color: #333;
}

.logo {
  color: #667eea;
  margin-right: 12px;
}

.site-title {
  font-size: 1.5rem;
  font-weight: 700;
  margin: 0;
}

.navbar-links {
  display: flex;
  gap: 30px;
}

.nav-link {
  text-decoration: none;
  color: #333;
  font-size: 1rem;
  font-weight: 500;
  padding: 20px 0;
  position: relative;
  transition: color 0.3s ease;
}

.nav-link:hover {
  color: #667eea;
}

.nav-link::after {
  content: '';
  position: absolute;
  bottom: 0;
  left: 0;
  width: 0;
  height: 2px;
  background-color: #667eea;
  transition: width 0.3s ease;
}

.nav-link:hover::after {
  width: 100%;
}

.navbar-actions {
  display: flex;
  align-items: center;
}

.btn-login {
  background-color: #667eea;
  color: white;
  padding: 10px 24px;
  border-radius: 5px;
  text-decoration: none;
  font-weight: 600;
  transition: background-color 0.3s ease;
}

.btn-login:hover {
  background-color: #5a67d8;
}

/* 用户菜单 */
.user-menu {
  position: relative;
}

.user-menu-button {
  display: flex;
  align-items: center;
  gap: 10px;
  background: none;
  border: none;
  cursor: pointer;
  padding: 8px 12px;
  border-radius: 8px;
  transition: background-color 0.3s ease;
}

.user-menu-button:hover {
  background-color: #f8f9fa;
}

.avatar-placeholder {
  width: 32px;
  height: 32px;
  border-radius: 50%;
  background-color: #667eea;
  display: flex;
  align-items: center;
  justify-content: center;
  color: white;
  font-weight: 600;
  font-size: 0.9rem;
}

.user-name {
  color: #333;
  font-weight: 500;
}

.user-dropdown {
  position: absolute;
  top: 100%;
  right: 0;
  margin-top: 5px;
  background-color: white;
  border-radius: 8px;
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.15);
  overflow: hidden;
  min-width: 180px;
}

.dropdown-item {
  display: block;
  width: 100%;
  padding: 12px 20px;
  border: none;
  background-color: transparent;
  cursor: pointer;
  text-align: left;
  text-decoration: none;
  color: #333;
  transition: background-color 0.3s ease;
}

.dropdown-item:hover {
  background-color: #f8f9fa;
  color: #667eea;
}

.dropdown-item.logout:hover {
  color: #e53e3e;
}

/* 响应式设计 */
@media (max-width: 992px) {
  .navbar-links {
    gap: 20px;
  }
}

@media (max-width: 768px) {
  .navbar-links {
    display: none;
  }
}
</style>